<template>
  <div class="userinfo-container">
    <el-card style="width: 45%; text-align: center; margin-left: 28%; height:87vh;">
      <div style="text-align: left; margin: 0 0 0 0;">
        <span class="textShadow" style="font-size: 26px;color: black;font-weight: bold">个人信息</span>
      </div>
      <div style="margin: 15px;  text-align: center;">
        <el-upload class="avatar-uploader"
                   action="/api/file/upload"
                   :show-file-list="false"
                   :before-upload="beforeFileUpload"
                   :on-success="handleAvatarSuccess"
                   :headers="{ token: user.token }">
          <img v-if="user.userPic" :src="user.userPic" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <el-form :model="user" :rules="rules" label-width="80px" style="padding-right: 10px;">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" placeholder="请输入用户名" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="name">
          <el-input v-model="user.nickname" placeholder="*"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="user.email" placeholder="*@qq.com"></el-input>
        </el-form-item>
        <div style="text-align: center; margin-top: 20px;">
          <el-button type="success" @click="update">保 存</el-button>
          <el-button type="primary" @click="reBack">返 回</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: JSON.parse(sessionStorage.getItem('user-key' || '{}')),
      // 添加数据对话框是否展示的标记
      rules: {
        nickname: [{
          required: true,
          message: '请输入昵称',
          trigger: 'blur'
        }],
        email: [{
          required: true,
          message: '请输入电子邮箱',
          trigger: 'blur'
        }]
      }
    }
  }, methods: {
    beforeFileUpload(file) {
      // 检查文件是否存在
      if (!file) {
        this.$message.error('请选择要上传的文件！');
        return false; // 阻止文件上传
      }
      // 检查文件类型和大小（这里只是示例，你可能需要更复杂的逻辑）
      const isJPGorPNG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isJPGorPNG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!');
        return false;
      }
      if (!isLt10M) {
        this.$message.error('上传图片大小不能超过 10MB!');
        return false;
      }
      // 如果通过检查，则返回 true 表示可以继续上传
      return true;
    },
    handleAvatarSuccess(res) {
      // 把user的头像属性换成上传的图片链接
      this.user.userPic = res.data
      this.update()
    },
    update() {
      //保存当前的用户信息到数据库
      this.$request.put('/user/updateInfo', this.user).then(res => {
        if (res.code === 200) {
          //成功更新
          this.$message.success('保存成功')
          setTimeout(function () {
            location.reload()
          }, 0)
        } else {
          this.$message.error(res.msg)
        }
      })
      // 更新浏览器缓存里的用户信息
      sessionStorage.setItem('user-key', JSON.stringify(this.user))

      //触发父级的数据更新
      this.$emit('updateUser', this.user)
    },
    reBack() {
      this.$router.push('/space/spaceHome')
    },
  }
}
</script>

<style scoped>
div /deep/ .el-form-item__label {
  font-weight: bold;
  font-size: 12px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 50%;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 50%;
}

div /deep/ .el-upload {
  border-radius: 50%;
}
</style>
